package com.lemonfish.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lemonfish.pojo.entity.CateParams;
import com.lemonfish.pojo.entity.GoodsParam;
import com.lemonfish.pojo.vo.GoodsParamVO;
import com.lemonfish.service.CateParamsService;
import com.lemonfish.service.GoodsParamService;
import com.lemonfish.util.MyJsonResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 分类参数 相关Controller
 *
 * @author Lemonfish
 * @version 1.0
 * @date 2020-03-09
 * Copyright © Chao Zhang
 */
@Api(tags = {"分类参数"})
@RestController
@RequestMapping("/goodsParam")
@Slf4j
public class GoodsParamController extends BaseCrudMappingRestController<GoodsParam, GoodsParamVO> {
    @Autowired
    CateParamsService cParamsService;
    @Autowired
    GoodsParamService gParamService;

    @PostMapping("/insert")
    public MyJsonResult insert(@RequestBody GoodsParam goodsParam, @RequestParam("cateId") long cateId) {
        System.out.println(goodsParam.getParentId());
        // 创建参数
        gParamService.insertParam(goodsParam);
        System.out.println(goodsParam.getParentId());

        // 创建联系
        cParamsService.createEntity(new CateParams(goodsParam.getId(), cateId));

        return MyJsonResult.success(null);
    }

    @PutMapping("/update")
    public MyJsonResult update(@RequestBody GoodsParam goodsParam) {
        // 更新参数
        goodsParam.setUpdateTime(new Date());
        gParamService.updateEntity(goodsParam);

        return MyJsonResult.success(null);
    }

    @DeleteMapping("/delete/{cateId}/{paramId}")
    public MyJsonResult delete(@PathVariable("cateId") long cateId,
                               @PathVariable("paramId") long paramId) {
        // 删除联系
        LambdaQueryWrapper<CateParams> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CateParams::getGoodsCateId, cateId);
        lqw.eq(CateParams::getGoodsParamId, paramId);
        cParamsService.deleteEntities(lqw);
        // 删除参数
        gParamService.deleteEntity(paramId);

        System.out.println("删除联系");

        return MyJsonResult.success(null);
    }


}
